package com.stoken.service.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stoken.dao.MemberAchievementDao;
import com.stoken.domain.MemberAchievement;
import com.stoken.domain.MemberBaseInfo;
import com.stoken.service.MemberAchievementService;
import com.stoken.service.MemberBaseInfoService;
import com.symbio.dao.hibernate.GenericDaoImpl;
import com.symbio.service.impl.AbstractServiceImpl;


@Service("MemberAchievementService")
public class MemberAchievementServiceImpl extends AbstractServiceImpl<MemberAchievement, Long> implements MemberAchievementService{
	
    @Autowired
	private MemberAchievementDao memberAchievementDao;
    
    @Autowired
	private MemberBaseInfoService memberBaseInfoService;
    
	@Override
	public GenericDaoImpl<MemberAchievement, Long> getDao() {		
		return memberAchievementDao;
	}

	@Override
	public List findAchievement(String baseUsername, int page) {
		MemberBaseInfo commend = memberBaseInfoService.findBaseByUserName("from MemberBaseInfo where baseUsername = ? ", baseUsername);
		long cId =  commend.getId();
		Session session = memberAchievementDao.getSessionFactory().getCurrentSession();
		String sql = "select b.base_username,b.base_name,r.register_grade,a.achievement_left_new,a.achievement_right_new,a.achievement_left_total,a.achievement_right_total " +
				     "from member_register_info r, member_base_info b, member_achievement a " +
				     "where r.base_username = b.base_username and b.id = a.base_username and r.register_commend_username = "+cId;       
		SQLQuery query = session.createSQLQuery(sql);
		List list = query.list();
		return list;
	}

	@Override
	public List findAchievementByPUsername(long pid) {
		Session session = memberAchievementDao.getSessionFactory().getCurrentSession();
		String sql = "select b.base_username,b.base_name,r.register_grade,a.achievement_left_new,a.achievement_right_new,a.achievement_left_total,a.achievement_right_total,r.register_location " +
				     "from member_register_info r, member_base_info b, member_achievement a " +
				     "where r.base_username = b.base_username and b.id = a.base_username and r.register_avtivestate = 1 and r.parent_username = "+pid;       
		SQLQuery query = session.createSQLQuery(sql);
		List list = query.list();
		return list;
	}

	@Override
	public MemberAchievement findSelf(long id) {
		return memberAchievementDao.find("baseUsername.id", id);
	}
	
	

}

